package com.atguigu.ssyx.acl.controller;

import com.atguigu.ssyx.acl.service.AdminRoleService;
import com.atguigu.ssyx.acl.service.AdminService;
import com.atguigu.ssyx.acl.service.RoleService;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.utils.MD5;
import com.atguigu.ssyx.model.acl.Admin;
import com.atguigu.ssyx.model.acl.AdminRole;
import com.atguigu.ssyx.model.acl.Role;
import com.atguigu.ssyx.vo.acl.AdminQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.rmi.MarshalledObject;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Api(tags = "用户管理")
@RestController
@RequestMapping("/admin/acl/user")
//@CrossOrigin //跨域

@RequiredArgsConstructor
public class AdminController {
    private final AdminService adminService;
    private final RoleService roleService;
    private final AdminRoleService adminRoleService;



  /*  url: `${api_name}/${page}/${limit}`,
    method: 'get',
    params: searchObj*/

    @ApiOperation("分页查询用户列表")
    @GetMapping("/{page}/{limit}")
    public Result pageUser(

            @ApiParam(name = "page", value = "当前页码", required = true) @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true) @PathVariable Long limit,
            AdminQueryVo adminQueryVo

    ) {


        //1.创建分页对象
        Page<Admin> p = new Page<>(page, limit);
        //2.创建分页条件
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        String username = adminQueryVo.getUsername();
        String name = adminQueryVo.getName();
        wrapper.like(username != null && !username.isEmpty(), Admin::getUsername, username)
                .like(name != null && !name.isEmpty(), Admin::getName, name);

        //3.执行分页查询
        adminService.page(p, wrapper);
        return Result.ok(p);

    }


  /*  url: `${api_name}/get/${id}`,
    method: 'get'*/

    @ApiOperation("根据id获取用户信息")
    @GetMapping("/get/{id}")
    public Result getUserById(
            @ApiParam(name = "id", value = "用户ID", required = true) @PathVariable Long id
    ) {
        Admin admin = adminService.getById(id);
        return Result.ok(admin);
    }


/*    url: `${api_name}/save`,
    method: 'post',
    data: user*/

    @PostMapping("/save")
    @ApiOperation("新增用户")
    public Result saveAdmin(@RequestBody Admin admin) {
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        boolean result = adminService.save(admin);
        if (!result) {
            return Result.fail("新增失败!");
        }

        return Result.ok(null);
    }

    /* url: `${api_name}/update`,
     method: 'put',
     data: user*/
    @ApiOperation("更新用户")
    @PutMapping("/update")
    public Result updateAdmin(@RequestBody Admin admin) {
        admin.setPassword(MD5.encrypt(admin.getPassword()));
        boolean result = adminService.updateById(admin);

        if (!result) {
            return Result.fail("更新失败!");
        }
        return Result.ok(null);
    }
/*
    url: `${api_name}/remove/${id}`,
    method: 'delete'*/

    @ApiOperation("根据id删除用户")
    @DeleteMapping("/remove/{id}")
    public Result removeAdmin(
            @ApiParam(name = "id", value = "用户id", required = true) @PathVariable Long id
    ) {
        adminService.removeById(id);
        return Result.ok(null);
    }

    /*    url: `${api_name}/batchRemove`,
        method: 'delete',
        data: ids*/
    @ApiOperation("批量删除用户")
    @DeleteMapping("/batchRemove")
    public Result batchRemoveAdmin(
            @ApiParam(name = "ids", value = "用户ids数组", required = true) @RequestBody Long[] ids
    ) {
        adminService.removeByIds(List.of(ids));
        return Result.ok(null);
    }

/*    url: `${api_name}/toAssign/${adminId}`,
    method: 'get'*/

    @ApiOperation("根据用户id查询所有角色")
    @GetMapping("/toAssign/{adminId}")

    public Result toAssign(
            @ApiParam(name = "adminId", value = "用户id", required = true) @PathVariable Long adminId) {

        //1.查询所有角色
        //2.查询用户分配的角色列表
        Map<String, Object> map = roleService.findRoleByUserId2(adminId);
        return Result.ok(map);


    }

    /*   url: `${api_name}/doAssign`,
       method: 'post',
       params: {
           adminId,
           roleId
       }*/
    @ApiOperation("根据用户分配角色")
    @PostMapping("/doAssign")


    public Result assignRoles(
            @ApiParam(name = "adminId", value = "用户id", required = true) @RequestParam Long adminId,
            @ApiParam(name = "roleId", value = "角色ids集合", required = true) @RequestParam("roleId") Long[] roleIds
    ) {
        roleService.saveBatchAdminRole(adminId, roleIds);
        return Result.ok(null);

    }
}

